<?php

namespace app\common\business;

use app\common\lib\Check;
use app\common\lib\Str;
use app\common\lib\Time;
use app\common\model\mysql\Worker as WorkerModel;
use think\Exception;

class Worker
{
    public $workerObj = null;
    public function __construct()
    {
        $this->workerObj = new WorkerModel();
    }

    //配送端登录
    public function workerLogin($data){
        if(!Check::validatePhoneNumber($data['phone'])){
            throw new Exception("手机号格式不正确");
        }
        //需要判断表 是否有 用户记录 phone
        //生成token
        $workerData = $this->workerObj->getWorkerByPhoneFind($data['phone']);
        if(empty($workerData)){
            throw new Exception("不存在该用户");
        }
        //判断密码是否正确
        /*if($user['password'] != md5(config("redis.password_salt").$data['password'])){
            throw new Exception("密码错误");
        }*/
        if($workerData['loginpwd'] != $data['password']){
            throw new Exception("密码错误");
        }
        $typeenum = Check::checkPermissions($workerData['typeenum']);
        if(!in_array("2",$typeenum)){
            throw new Exception("该员工类型不正确");
        }
        if($workerData['stateenum'] == 8){
            throw new Exception("该员工已离职");
        }
        if($workerData['isdel'] == 1){
            throw new Exception("该员工已删除");
        }
        //需要记录信息到mysql表中
        $worker_name = $workerData['tname'];
        $token = Str::getLoginToken($worker_name);
        $updateData = [
            'token'=>$token,
        ];
        $res = $this->workerObj->updateByWorkerId($workerData['worker_id'],$updateData);
        if(empty($res)){
            throw new Exception("登录失败");
        }
        $redisData = [
            "worker_id"     => $workerData['worker_id'],
            "worker_name"   => $worker_name,
            "worker_phone"  => $workerData['phone'],
            "company_id"    => $workerData['company_id'],
            "shop_id"       => $workerData['shop_id'],
            "jpush"         => md5($workerData['worker_id']),
        ];
        $res = cache(config("redis.token_pre").$token,$redisData,Time::workerLoginExpiresTime());
        return $res ? ["token"=>$token,"worker_name"=>$worker_name,"worker_phone"=>$workerData['phone']] : false;
    }

    //库管端登录
    public function KeeperLogin($data){
        if(!Check::validatePhoneNumber($data['phone'])){
            throw new Exception("手机号格式不正确");
        }
        //需要判断表 是否有 用户记录 phone
        //生成token
        $workerData = $this->workerObj->getWorkerByPhoneFind($data['phone']);
        if(empty($workerData)){
            throw new Exception("不存在该用户");
        }
        //判断密码是否正确
        /*if($user['password'] != md5(config("redis.password_salt").$data['password'])){
            throw new Exception("密码错误");
        }*/
        if($workerData['loginpwd'] != $data['password']){
            throw new Exception("密码错误");
        }
        $typeenum = Check::checkPermissions($workerData['typeenum']);
        if(!in_array("32",$typeenum) && !in_array("256",$typeenum)){
            throw new Exception("该员工类型不正确");
        }
        if($workerData['stateenum'] == 8){
            throw new Exception("该员工已离职");
        }
        if($workerData['isdel'] == 1){
            throw new Exception("该员工已删除");
        }
        //需要记录信息到mysql表中
        $worker_name = $workerData['tname'];
        $token = Str::getLoginToken($worker_name);
        $updateData = [
            'token'=>$token,
        ];
        $res = $this->workerObj->updateByWorkerId($workerData['worker_id'],$updateData);
        if(empty($res)){
            throw new Exception("登录失败");
        }
        $redisData = [
            "worker_id"     => $workerData['worker_id'],
            "worker_name"   => $worker_name,
            "worker_phone"  => $workerData['phone'],
            "company_id"    => $workerData['company_id'],
            "shop_id"       => $workerData['shop_id'],
            "typeenum"      => $typeenum,
            "jpush"         => md5($workerData['worker_id']),
        ];
        $res = cache(config("redis.token_pre").$token,$redisData,Time::workerLoginExpiresTime());
        return $res ? ["token"=>$token,"worker_name"=>$worker_name,"worker_phone"=>$workerData['phone'],"typeenum"=>$typeenum] : false;
    }
}
